package com.lt.accesscontrolmanager.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.lt.accesscontrolmanager.domain.AccessControlDevice;
import com.lt.accesscontrolmanager.domain.Building;
import com.lt.accesscontrolmanager.service.AccessControlDeviceSerivce;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/devices")
@CrossOrigin(origins = "http://localhost:5173/") // 允许的源
public class DeviceController {

    @Autowired
    private AccessControlDeviceSerivce deviceService;



    // 分页查询设备信息
    @GetMapping("/search")
    public Page<AccessControlDevice> searchDevices(
            @RequestParam String buildingId,
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "5") int pageSize) {

        return deviceService.getDevicesByBuildingName(buildingId, pageNum, pageSize);
    }

    // 添加设备
    @PostMapping("/add")
    public ResponseEntity<String> addDevice(@RequestBody AccessControlDevice device) {
        System.out.println("aaaaaaaaaaa");
        deviceService.addDevice(device);
        return ResponseEntity.ok("Device added successfully.");
    }

    // 更新设备状态
    @PostMapping("/update-status")
    public ResponseEntity<String> updateDeviceStatus(@RequestParam Long id, @RequestParam int status) {
        deviceService.updateDeviceStatus(id, status);
        return ResponseEntity.ok("Device status updated.");
    }


}
